package com.dstz.bpm.engine.data.handle;

import cn.hutool.core.map.MapUtil;
import com.dstz.base.api.exception.BusinessError;
import com.dstz.base.core.util.StringUtil;
import com.dstz.base.db.dboper.DbOperator;
import com.dstz.base.db.dboper.DbOperatorFactory;
import com.dstz.bpm.api.engine.action.cmd.BaseActionCmd;
import com.dstz.bpm.api.exception.BpmStatusCode;
import com.dstz.bpm.api.model.def.BpmDataModel;
import com.dstz.bpm.api.model.nodedef.BpmNodeDef;
import com.dstz.bpm.api.service.BpmProcessDefService;
import com.dstz.bpm.api.service.BpmRightsFormService;
import com.dstz.bpm.core.manager.BpmBusLinkManager;
import com.dstz.bpm.core.manager.BpmInstanceManager;
import com.dstz.bpm.core.model.BpmBusLink;
import com.dstz.bpm.core.model.BpmInstance;
import com.dstz.bpm.engine.model.DefaultBpmProcessDef;
import com.dstz.bus.api.model.IBusinessData;
import com.dstz.bus.api.model.IBusinessObject;
import com.dstz.bus.api.model.IBusinessPermission;
import com.dstz.bus.api.service.IBusinessDataService;
import com.dstz.bus.api.service.IBusinessObjectService;
import com.dstz.bus.api.service.IBusinessPermissionService;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class BpmBusDataHandle implements IBpmBusDataHandle {
    @Resource
    private BpmInstanceManager f;
    @Resource
    private BpmProcessDefService a;
    @Resource
    private BpmBusLinkManager l;
    @Resource
    private BpmRightsFormService aW;
    @Resource
    private IBusinessDataService aD;
    @Autowired
    private IBusinessObjectService n;
    @Autowired
    private IBusinessPermissionService aX;
    private static int aY = -1;
    private static Set<String> aZ = Collections.synchronizedSet(new HashSet());
    private static final String tableName = "BPM_BUS_LINK";

    public Map<String, IBusinessData> getInstanceData(IBusinessPermission businessPermission, BpmInstance instance) {
        Map<String, IBusinessData> dataMap = null;
        BpmInstance topInstance = this.f.getTopInstance(instance);
        if (topInstance != null) {
            dataMap = this.getInstanceBusData(topInstance.getId(), businessPermission);
        }

        if (dataMap == null) {
            dataMap = new HashMap();
        }

        ((Map) dataMap).putAll(this.getInstanceBusData(instance.getId(), businessPermission));
        DefaultBpmProcessDef processDef = (DefaultBpmProcessDef) this.a.getBpmProcessDef(instance.getDefId());
        List<BpmDataModel> listDataModel = processDef.getDataModelList();
        Iterator var7 = listDataModel.iterator();

        while (var7.hasNext()) {
            BpmDataModel model = (BpmDataModel) var7.next();
            String code = model.getCode();
            if (!((Map) dataMap).containsKey(code)) {
                IBusinessObject businessObject = this.n.getFilledByKey(code);
                businessObject.setPermission(businessPermission.getBusObj(code));
                IBusinessData busData = this.aD.loadData(businessObject, (Object) null);
                ((Map) dataMap).put(code, busData);
            }
        }

        return (Map) dataMap;
    }

    public Map<String, IBusinessData> getInstanceBusData(String instanceId, IBusinessPermission businessPermission) {
        List<BpmBusLink> busLinks = this.l.getByInstanceId(instanceId);
        Map<String, IBusinessData> dataMap = new HashMap();
        Iterator var5 = busLinks.iterator();

        while (var5.hasNext()) {
            BpmBusLink busLink = (BpmBusLink) var5.next();
            IBusinessObject businessObject = this.n.getFilledByKey(busLink.getBizCode());
            if (businessPermission != null) {
                businessObject.setPermission(businessPermission.getBusObj(busLink.getBizCode()));
            }

            IBusinessData busData = this.aD.loadData(businessObject, busLink.getBizId());
            if (busData == null) {
                throw new BusinessError(String.format("bizCode[%s] bizId[%s]", busLink.getBizCode(), busLink.getBizId()), BpmStatusCode.FLOW_BUS_DATA_LOSE);
            }

            dataMap.put(busLink.getBizCode(), busData);
        }

        return dataMap;
    }

    public Map<String, IBusinessData> getInitData(IBusinessPermission businessPermision, String defId) {
        Map<String, IBusinessData> dataMap = new HashMap();
        DefaultBpmProcessDef processDef = (DefaultBpmProcessDef) this.a.getBpmProcessDef(defId);
        List<BpmDataModel> listDataModel = processDef.getDataModelList();
        Iterator var6 = listDataModel.iterator();

        while (var6.hasNext()) {
            BpmDataModel model = (BpmDataModel) var6.next();
            String code = model.getCode();
            IBusinessObject businessObject = this.n.getFilledByKey(code);
            businessObject.setPermission(businessPermision.getBusObj(code));
            IBusinessData busData = this.aD.loadData(businessObject, (Object) null, true);
            dataMap.put(code, busData);
        }

        return dataMap;
    }

    public void o(BaseActionCmd actionCmd) {
        Map<String, IBusinessData> boDataMap = actionCmd.getBizDataMap();
        if (!MapUtil.isEmpty(boDataMap)) {
            BpmInstance instance = (BpmInstance) actionCmd.getBpmInstance();
            String nodeId = actionCmd.getNodeId();
            if (StringUtil.isEmpty(nodeId)) {
                BpmNodeDef startNode = this.a.getStartEvent(instance.getDefId());
                nodeId = startNode.getNodeId();
            }

            IBusinessPermission businessPermision = this.aW.getNodeSavePermission(instance.getDefKey(), nodeId, boDataMap.keySet());
            BpmInstance topInstance = this.f.getTopInstance(instance);
            Set<String> topModelCodes = new HashSet();
            Iterator var10;
            BpmDataModel dataModel;
            String modelCode;
            IBusinessData businessData;
            if (topInstance != null) {
                DefaultBpmProcessDef topDef = (DefaultBpmProcessDef) this.a.getBpmProcessDef(topInstance.getDefId());
                List<BpmBusLink> topBusLinks = this.l.getByInstanceId(topInstance.getId());
                var10 = topDef.getDataModelList().iterator();

                while (var10.hasNext()) {
                    dataModel = (BpmDataModel) var10.next();
                    modelCode = dataModel.getCode();
                    if (boDataMap.containsKey(modelCode)) {
                        topModelCodes.add(modelCode);
                        businessData = (IBusinessData) boDataMap.get(modelCode);
                        businessData.getBusTableRel().getBusObj().setPermission(businessPermision.getBusObj(modelCode));
                        this.aD.saveData(businessData);
                        this.a(businessData, modelCode, topInstance, topBusLinks);
                    }
                }
            }

            List<BpmBusLink> busLinkList = this.l.getByInstanceId(instance.getId());
            DefaultBpmProcessDef bpmProcessDef = (DefaultBpmProcessDef) this.a.getBpmProcessDef(instance.getDefId());
            var10 = bpmProcessDef.getDataModelList().iterator();

            while (var10.hasNext()) {
                dataModel = (BpmDataModel) var10.next();
                modelCode = dataModel.getCode();
                if (boDataMap.containsKey(modelCode) && !topModelCodes.contains(modelCode)) {
                    businessData = (IBusinessData) boDataMap.get(modelCode);
                    businessData.getBusTableRel().getBusObj().setPermission(businessPermision.getBusObj(modelCode));
                    this.aD.saveData(businessData);
                    this.a(businessData, modelCode, instance, busLinkList);
                }
            }

        }
    }

    private void a(IBusinessData iBusinessData, String modelCode, BpmInstance instance, List<BpmBusLink> busLinks) {
        Iterator var5 = busLinks.iterator();
        BpmBusLink link;
        do {
            if (!var5.hasNext()) {
                BpmBusLink busLink = new BpmBusLink();
                busLink.setBizCode(modelCode);
                busLink.setBizId(String.valueOf(iBusinessData.getPk()));
                busLink.setInstId(instance.getId());
                busLink.setDefId(instance.getDefId());
                busLink.setContent(iBusinessData.getData().toString());
                this.b(modelCode);
                this.l.create(busLink);
                return;
            }

            link = (BpmBusLink) var5.next();
        } while (!link.getBizId().equals(iBusinessData.getPk()));
    }


    private void b(String partName) {
        DbOperator dbOperator = DbOperatorFactory.getLocal();
        if (!StringUtil.isEmpty(partName)) {
            boolean isPartExist;
            if (aY == -1) {
                isPartExist = dbOperator.supportPartition("BPM_BUS_LINK");
                aY = isPartExist ? 1 : 0;
            }

            if (aY != 0) {
                if (!aZ.contains(partName)) {
                    isPartExist = dbOperator.isExsitPartition("BPM_BUS_LINK", partName);
                    if (isPartExist) {
                        aZ.add(partName);
                    } else {
                        dbOperator.createPartition("BPM_BUS_LINK", partName);
                        aZ.add(partName);
                    }
                }
            }
        }
    }
}
